<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<%@ taglib uri="/WEB-INF/tld/fmt.tld" prefix="fmt" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <c:set var="ctx" value="${pageContext.request.contextPath}"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>结清预览页面</title>
    <jsp:include page="../../import.jsp" flush="true"/>
    <style>
        .align-center {
            margin: 0 auto;
            width: 500px;
            text-align: center;
        }
    </style>
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div id="pageInTab" class="pageInTab">
        <form id="formSet1">
            <div class="fieldset1 tabel_col clearFix">
                <div class="fieldBox col_50">
                    <div class="label">融资租赁合同号</div>
                    <div class="field">
                        ${pay.lsCntNo}
                    </div>
                </div>
                <div class="fieldBox col_50">
                    <div class="label">承租人</div>
                    <div class="field">
                        ${pay.cstNm}
                    </div>
                </div>
                <div class="fieldBox col_50">
                    <div class="label">支付表号</div>
                    <div class="field">
                        <a href="javascript:detailBtn()" class="link">
                            ${pay.paySchNo}
                        </a>
                    </div>
                </div>
                <div class="fieldBox col_50">
                    <div class="label">结清方式</div>
                    <div class="field">
                        <span id="settleWay" name="settleWay">${pay.settleTypeNm}</span>
                    </div>
                </div>
                <div class="fieldBox col_50">
                    <div class="label">开始期次</div>
                    <div class="field">
                        ${pay.settlePd == 0?'到期':pay.settlePd}
                    </div>
                </div>
                <div class="fieldBox col_50">
                    <div class="label" role="ligertip">自结清期应还租金合计</div>
                    <div class="field">
                        ${pay.surplusRentTotalStl}
                    </div>
                </div>

                <div class="fieldBox col_50">
                    <div class="label" role="ligertip">结清开始前每期应还租金合计</div>
                    <div class="field">
                        ${pay.rentBeforeSettleTotal}
                    </div>
                </div>

                <div class="fieldBox col_50">
                    <div class="label">核算金额校验</div>
                    <div class="field">
                        ${pay.checkbitAmt}
                    </div>
                </div>

                <div class="fieldBox col_100">
                    <div class="label">最终应还金额公式</div>
                    <div class="field">
                        <span style="color: red">结清前应还的每期租金合计 + 自结清期剩余租金合计 + 逾期罚息 + 其他费用 - 保证金 - 保险保证金 - 逾期当期已还款金额 + 未核销借款金额</span>
                    </div>
                </div>


                <div class="caption">
                    <span class="title">已付款项明细信息</span>
                </div>
                <div class="fieldBox col_100">
                    <div class="sqPanel">
                        <div id="ligergrid_fee" class="sq-grid"></div>
                    </div>
                    <div class="sqPanel">
                        <div id="ligergrid_blf" class="sq-grid sq-grid-noTitle"></div>
                    </div>
                </div>

                <div class="caption">
                    <span class="title">结清款项信息</span>
                </div>
                <div class="fieldBox col_100">
                    <div class="sqPanel">
                        <div id="ligergrid_shouldPay" class="sq-grid sq-grid-noTitle"></div>
                    </div>
                </div>

                <div class="fieldBox col_100">
                    <div class="label">应收结清款</div>
                    <div class="field">
                        <span id="totalSettAmt" style="display: inline-block; font-size: 16px;"></span>
                    </div>
                </div>

            </div>

            <%--附件--%>
            <jsp:include page="../../settlement/settleFile.jsp" flush="true">
                <jsp:param name="pkId" value="${pay.paySchId}"/>
                <jsp:param name="filePath" value="prjprdpayschm"/>
                <jsp:param name="optAuth" value="1"/>
            </jsp:include>

            <div class="btnsBox align-center">
                <span class="sq-button sq-button-round" onclick="startSettleFlow()">发起结清</span>
            </div>
        </form>
    </div>
</div>

<script type="text/javascript">
    var basePath = '<%=path%>';
    var paySchId = '${pay.paySchId}';
    var settlePd = '${pay.settlePd}';
    $(function () {
        $("#totalSettAmt").html(renderAmt(null, null, ${pay.totalSettleAmt}));
        initFeeDataGrid();
        initBlfDataGrid();
        initShouldPayDataGrid();
    });

    // 初始化费用项grid
    function initFeeDataGrid() {
        var feeListPage = JSON.parse('${feeListPage}');
        $("#ligergrid_fee").ligerGrid({
            data: feeListPage,
            columns: [
                {display: '项目', name: 'feeTypCdNm', minWidth: 140},
                {display: '应收金额', name: 'feeAmt', minWidth: 140, type: 'currency', align: 'right', render: renderAmt},
                {display: '已收金额', name: 'paidUpAmt', minWidth: 60, type: 'currency', align: 'right', render: renderAmt},
                {display: '未收金额', name: 'arAmt', minWidth: 60, type: 'currency', align: 'right', render: renderAmt}
            ],
            usePager: false,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: 300,
            heightDiff: 0,
            isScroll: false,
            checkbox: false,
            rownumbers: true,
            allowUnSelectRow: false,
            alternatingRow: true
        });
    }

    // 初始化本利罚grid
    function initBlfDataGrid() {
        var blfListPage = JSON.parse('${blfListPage}');
        $("#ligergrid_blf").ligerGrid({
            data: blfListPage,
            columns: [
                {display: '项目', name: 'feeTypCdNm', minWidth: 140,
                    render: function (rowdata, rowindex, value) {
                        if (rowdata.checkbit == 1) {
                            return '<div style="color: #f02b2b;">'+value+'</div>';
                        }
                        return value;
                    }
                },
                {display: '应收金额', name: 'feeAmt', minWidth: 140, render: renderAmt, type: 'currency', align: 'right',
                    totalSummary: {
                        render: function (suminf, column, cell) {
                            var h='<div>合计：￥' + FormatMoney((parseFloat(suminf.sum)).toFixed(2)) + '</div>'
                            return h;
                        }
                    }
                },
                {display: '已收金额', name: 'paidUpAmt', minWidth: 60, render: renderAmt, type: 'currency', align: 'right',
                    totalSummary: {
                        render: function (suminf, column, cell) {
                            return '<div>合计：￥' + FormatMoney((parseFloat(suminf.sum)).toFixed(2)) + '</div>';
                        }
                    }
                },
                {display: '未收金额', name: 'arAmt', minWidth: 60, render: renderAmt, type: 'currency', align: 'right',
                    totalSummary: {
                        render: function (suminf, column, cell) {
                            return '<div>合计：￥' + FormatMoney((parseFloat(suminf.sum)).toFixed(2)) + '</div>';
                        }
                    }
                }
            ],
            usePager: false,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            heightDiff: 0,
            isScroll: false,
            checkbox: false,
            rownumbers: true,
            frozen: false,
            allowUnSelectRow: false,
            alternatingRow: true
        });
    }

    // 初始化应还grid
    function initShouldPayDataGrid() {
        var shouldPayListPage = JSON.parse('${shouldPayListPage}');
        $("#ligergrid_shouldPay").ligerGrid({
            data: shouldPayListPage,
            columns: [
                {display: '费用科目', name: 'feeTypCdNm', minWidth: 140},
                {
                    display: '金额', name: 'feeAmt', minWidth: 140, align: 'right',
                    render: function (item) {
                        var amt = item.feeAmt;
                        if (item.capFlow == ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT) {
                            amt = "<span style='color:red'>" +
                                "￥" + FormatMoney((parseFloat(item.feeAmt)).toFixed(2))
                                + "</span>";
                        } else {
                            amt = "￥" + FormatMoney((parseFloat(item.feeAmt)).toFixed(2));
                        }
                        return amt;
                    }
                }
            ],
            usePager: false,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            heightDiff: -2,
            isScroll: false,
            checkbox: false,
            frozen: false,
            allowUnSelectRow: false,
            alternatingRow: true,
            groupColumnName: 'capFlow',
            groupRender: function (capFlow, groupdata) {
                if (capFlow == ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT) {
                    return "<span style='font-size: 14px;'>应退款项</span>";
                } else if (capFlow == ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_IN) {
                    return "<span style='font-size: 14px;'>应收款项</span>";
                } else {
                    return capFlow;
                }
            }
        });
    }

    function startSettleFlow() {
        $.ligerDialog.confirm("确定要发起结清流程吗？", function (flag) {
            if (flag == false) return;
            var data = {};
            data["paySchId"] = paySchId;
            data["settlePd"] = settlePd;
            var wait_dialog;
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: data,
                url: basePath + '/settlement/bill/startFlow.do',
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                        queryStartResult();
                    } else {
                        UIUtils.alertErrorHold1(msg.info);
                    }
                }
            });
        });
    }

    // 轮询发起结果
    function queryStartResult() {
        var countDown = 10;
        var wait_dialog = $.ligerDialog.SQalert({
            content: "正在发起流程，请稍等</br>" + countDown + " 秒",
            SQ_type: "waitting"
        });
        var curInterval = setInterval(function () {
            $(".l-dialog-content-noimage").html('<span class="loadingGif"></span>正在发起流程，请稍等</br>' + (--countDown) + ' 秒');
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: {paySchId: paySchId},
                url: basePath + '/settlement/bill/queryStartResult.do',
                success: function (msg) {
                    if (msg.success == 1 && msg.data == 1 || countDown == 0) {
                        wait_dialog.close();
                        clearInterval(curInterval);
                        $.ligerDialog.SQalert({
                            content: msg.info, SQ_showButtons: true, SQ_type: "success", SQ_onBtnclick: function () {
                                Remove_navTab();
                            }
                        });
                    }
                }
            });
        }, 1000);
    }

    function detailBtn() {
        var prjId = '${pay.prjId}';
        var prjPrdDtlId = '${pay.prjPrdDtlId}';
        var prjPrdId = '${pay.prjPrdId}';
        var paySchId = '${pay.paySchId}';
        Add_navTab('tab_prj_quat_prps' + paySchId, '查看支付表', basePath + '/project/prjprdpayschm/toAddPage.do?prjId=' + prjId + '&&id=' + paySchId + '&&prjPrdDtlId=' + prjPrdDtlId + '&&prjPrdId=' + prjPrdId + '&&detailFlag=true');
    }

    //格式化为人民币格式
    function renderAmt(rowdata, rowindex, value) {
        if (null !== value && value !== 'undefined' && value !== "") {
            if (typeof Number(value) == 'number') {
                var h = "￥" + FormatMoney((parseFloat(value)).toFixed(2));
                return h;
            }
        }
    }
</script>
</body>
</html>
